package net.howson.phil.datahiding.scheme;

import java.util.Random;

import net.howson.phil.datahiding.stream.BitWidthStreamUtils;

public class DefaultFixedWidthEncodingScheme implements
		FixedWidthSingleValueEncodingScheme {

	private final int bitWidth;
	private final int mask;
	private final Random random;

	public DefaultFixedWidthEncodingScheme(int bitWidth, int seed) {
		this.bitWidth = bitWidth;
		this.mask = BitWidthStreamUtils.createMask(bitWidth);
		this.random = new Random(seed);
	}

	@Override
	public int decode(int input, int seed) {
		return (input & mask) ^ random.nextInt(mask);
	}

	@Override
	public int encode(int input, int existing) {
		return (existing & (~mask)) | ((input ^ random.nextInt(mask)) & mask);
	}

	@Override
	public int getBitWidth() {
		return bitWidth;
	}

	@Override
	public int getNumberOfBytesEncodable(int width, int height) {
		return (width * height * 3) / (8 / bitWidth);
	}

}
